package com.lul15834.orderfood.activity.user.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.lul15834.orderfood.R;
import com.lul15834.orderfood.activity.manager.adapter.OrderNoDetailAdapter;
import com.lul15834.orderfood.activity.user.UserBuyActivity;
import com.lul15834.orderfood.bean.FoodBean;
import com.lul15834.orderfood.bean.OrderDetailBean;
import com.lul15834.orderfood.bean.UserBean;
import com.lul15834.orderfood.dao.AdminDao;
import com.lul15834.orderfood.dao.CommentDao;
import com.lul15834.orderfood.dao.FoodDao;

import org.json.JSONException;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 显示商家商品的adapter
 */
public class BuyFoodAdapter extends RecyclerView.Adapter<BuyFoodAdapter.BuyFoodViewHolder> {
    private List<FoodBean> list;
    private Context context;
    private Context contextFather;
    JSONArray jsonArray;
    public BuyFoodAdapter( List<FoodBean> list,Context contextFather){
        this.list=list;
        this.contextFather=contextFather;
        jsonArray=new JSONArray();
        for (FoodBean foodBean : list) {
            com.alibaba.fastjson2.JSONObject jsonObject=new JSONObject();
            jsonObject.put("foodId",foodBean.getFoodId());
            jsonObject.put("num","0");
            jsonArray.add(jsonObject);


        }
    }
    @NonNull
    @Override
    public BuyFoodAdapter.BuyFoodViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View itemView=inflater.inflate(R.layout.list_user_buy_food_list,parent,false);
        return new BuyFoodAdapter.BuyFoodViewHolder(itemView);
    }
    @Override
    public void onBindViewHolder(@NonNull BuyFoodAdapter.BuyFoodViewHolder holder, int position) {
        FoodBean tem=list.get(position);
        //价格
        UserBuyActivity fatherView=(UserBuyActivity) contextFather;
        TextView priceZ=fatherView.findViewById(R.id.user_buy_business_price);
        TextView food=fatherView.findViewById(R.id.user_buy_business_food);//存放购买哪些商品
        String foodJson=food.getText().toString();

        if (foodJson.isEmpty()){//代表第一次向里面送东西
            //将所有商品都存放在food这个商家的
            food.setText(jsonArray.toString());
        }



        String businessId=tem.getBusinessId();//商家的账号
        UserBean businessUser=AdminDao.getBusinessUser(businessId);
        //月售统计当前月份的销售
        Bitmap bitmap= BitmapFactory.decodeFile(tem.getFoodImage());
        holder.img.setImageBitmap(bitmap);
        holder.name.setText(tem.getFoodName());
        holder.price.setText("价格"+tem.getFoodPrice());
        holder.des.setText("描述"+tem.getFoodDescription());
        int saleNum=FoodDao.getMouSaleNum(tem.getFoodId());
        holder.num.setText("月销:"+saleNum);
        BigDecimal sumPrice=new BigDecimal(priceZ.getText().toString());
        holder.add.setOnClickListener(new View.OnClickListener() {//加
            @Override
            public void onClick(View v) {
                String NuM=holder.Num.getText().toString();
                int sl=Integer.valueOf(NuM)+1;
                holder.Num.setText(String.valueOf(sl));//数量
                BigDecimal priceB=new BigDecimal(tem.getFoodPrice());//19.86
                BigDecimal sum=new BigDecimal(priceZ.getText().toString());//19.86*10 价格
                BigDecimal jgB=sum.add(priceB);//计算后的价格
                priceZ.setText(jgB.toString());
                String foodJson=food.getText().toString();//获取存放购买内容
                JSONArray z=JSONArray.parseArray(foodJson);
                JSONArray newJson=new JSONArray();
                for (Object o:z){
                    JSONObject temJSon=JSONObject.parseObject(o.toString());
                    if (temJSon.get("foodId").equals(tem.getFoodId())){
                        temJSon.put("num",holder.Num.getText());
                    }
                    newJson.add(temJSon);
                }
                food.setText(newJson.toJSONString());
            }
        });
        holder.sub.setOnClickListener(new View.OnClickListener() {//减
            @Override
            public void onClick(View v) {
                String NuM=holder.Num.getText().toString();
                int sl=Integer.valueOf(NuM)-1;
                if (sl>=0){
                    holder.Num.setText(String.valueOf(sl));
                    BigDecimal priceB=new BigDecimal(tem.getFoodPrice());
                    BigDecimal sum=new BigDecimal(priceZ.getText().toString());
                    BigDecimal jgB=sum.subtract(priceB);//计算后的价格
                    priceZ.setText(jgB.toString());
                    String foodJson=food.getText().toString();//获取存放购买内容
                    JSONArray z=JSONArray.parseArray(foodJson);
                    JSONArray newJson=new JSONArray();
                    for (Object o:z){
                        JSONObject temJSon=JSONObject.parseObject(o.toString());
                        if (temJSon.get("foodId").equals(tem.getFoodId())){
                            temJSon.put("num",holder.Num.getText());
                        }
                        newJson.add(temJSon);
                    }
                    food.setText(newJson.toJSONString());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class BuyFoodViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView name;
        TextView num;
        TextView price;
        TextView des;
        ImageView add;
        TextView Num;
        ImageView sub;
        public BuyFoodViewHolder(@NonNull View itemView) {
            super(itemView);
            img=itemView.findViewById(R.id.user_buy_food_list_foodImg);
            name=itemView.findViewById(R.id.user_buy_food_list_name);
            num=itemView.findViewById(R.id.user_buy_food_list_saleNum);
            price=itemView.findViewById(R.id.user_buy_food_list_price);
            des=itemView.findViewById(R.id.user_buy_food_list_des);
            add=itemView.findViewById(R.id.user_buy_food_list_add);
            Num=itemView.findViewById(R.id.user_buy_food_list_num);
            sub=itemView.findViewById(R.id.user_buy_food_list_sub);
        }
    }
}

